<?php
/**
 * Author:xsc
 * Time:2023/8/31 16:03
 */

namespace App\Repository;

use App\Models\MerchantsCasting;

class MerchantsCastingRepo extends BaseRepo
{

    /**
     * 根据商户id获取铸造信息
     * @param int $mer_id
     * @return array
     * Author:xsc
     * Time:2023/9/4 19:47
     */
    public function getByMerId(int $mer_id)
    {
        $res = MerchantsCasting::query()->where('mer_id', $mer_id)->first();
        return $res ? $res->toArray() : [];
    }

    /**
     * 更新创作者剩余铸造数
     * @param int $mer_id
     * @param int $num
     * @return mixed
     * Author:xsc
     * Time:2023/9/1 16:21
     */
    public function updateRemaining(int $mer_id, int $num)
    {
        return MerchantsCasting::query()->where('mer_id', $mer_id)->where('remaining','>=',$num)->decrement('remaining', $num);
    }

    /**
     * 更新创作者total总量
     * @param int $mer_id
     * @param int $num
     * @param string $type
     * @return int|void
     * @author shen.zk
     * @date 2023-10-30 16:02
     */
    public function updateTotal(int $mer_id, int $num,string $type)
    {

        if (!in_array($type,['add','reduce']))
            return false;
        if ($type == 'add')
        {
            return MerchantsCasting::query()->where('mer_id', $mer_id)->increment('total', $num);
        }
        if ($type == 'reduce')
        {
            return MerchantsCasting::query()->where('mer_id', $mer_id)->decrement('total', $num);
        }
    }

    /**
     * 创作者铸造数量增加
     * @param int $mer_id
     * @param int $num
     * @return int
     * @author shen.zk
     * @date 2023-10-26 17:13
     */
    public function updateIncRemaining(int $mer_id, int $num)
    {
        return MerchantsCasting::query()->where('mer_id', $mer_id)->increment('remaining', $num);
    }
}
